package com.ruoyi.privateExamination.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.privateExamination.domain.PrivateExamination;
import com.ruoyi.privateExamination.service.IPrivateExaminationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 我的体检记录Controller
 * 
 * @author ruoyi
 * @date 2025-08-16
 */
@RestController
@RequestMapping("/privateExamination/privateExamination")
public class PrivateExaminationController extends BaseController
{
    @Autowired
    private IPrivateExaminationService privateExaminationService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询我的体检记录列表
     */
    @PreAuthorize("@ss.hasPermi('privateExamination:privateExamination:list')")
    @GetMapping("/list")
    public TableDataInfo list(HttpServletRequest request,@RequestParam(required = false) String teamName, @RequestParam(required = false) Long examinationScore)
    {
        startPage();
        //从token中获取userId
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId =loginUser.getUserId();
        List<PrivateExamination> list = privateExaminationService.selectPrivateExaminationByUserId(userId, teamName, examinationScore);
        return getDataTable(list);
    }

    /**
     * 导出我的体检记录列表
     */
    @PreAuthorize("@ss.hasPermi('privateExamination:privateExamination:export')")
    @Log(title = "我的体检记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HttpServletRequest request,@RequestParam(required = false) String teamName, @RequestParam(required = false) Long examinationScore)
    {
        //从token中获取userId
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId =loginUser.getUserId();
        List<PrivateExamination> list = privateExaminationService.selectPrivateExaminationByUserId(userId, teamName, examinationScore);
        ExcelUtil<PrivateExamination> util = new ExcelUtil<PrivateExamination>(PrivateExamination.class);
        util.exportExcel(response, list, "我的体检记录数据");
    }

    /**
     * 获取我的体检记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('privateExamination:privateExamination:query')")
    @GetMapping(value = "/{examinationId}")
    public AjaxResult getInfo(@PathVariable("examinationId") Long examinationId)
    {
        return success(privateExaminationService.selectPrivateExaminationByExaminationId(examinationId));
    }

    /**
     * 新增我的体检记录
     */
    @PreAuthorize("@ss.hasPermi('privateExamination:privateExamination:add')")
    @Log(title = "我的体检记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PrivateExamination privateExamination,HttpServletRequest request)
    {   
        // 从token中获取userId
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId = loginUser.getUserId();
        privateExamination.setUserId(userId);
        return toAjax(privateExaminationService.insertPrivateExamination(privateExamination));
    }

    /**
     * 修改我的体检记录
     */
    @PreAuthorize("@ss.hasPermi('privateExamination:privateExamination:edit')")
    @Log(title = "我的体检记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PrivateExamination privateExamination)
    {
        return toAjax(privateExaminationService.updatePrivateExamination(privateExamination));
    }

    /**
     * 删除我的体检记录
     */
    @PreAuthorize("@ss.hasPermi('privateExamination:privateExamination:remove')")
    @Log(title = "我的体检记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{examinationIds}")
    public AjaxResult remove(@PathVariable Long[] examinationIds)
    {
        return toAjax(privateExaminationService.deletePrivateExaminationByExaminationIds(examinationIds));
    }
}
